package edu.pxxy.yzj.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import edu.pxxy.yzj.db.MyDBOpenHelper;
import edu.pxxy.yzj.domain.Food;

public class FoodDaoImpl implements FoodDao{

    private SQLiteDatabase sqLiteDatabase;

    private static FoodDao foodDao;

    private FoodDaoImpl(Context context){
        sqLiteDatabase=new MyDBOpenHelper(context).getWritableDatabase();
    }

    public static FoodDao getInstence(Context context){
        if(foodDao==null){
            System.out.println("getInstence");
            foodDao=new FoodDaoImpl(context);
        }
        return foodDao;
    }

    @Override
    public void addFood(Food food) {
        ContentValues contentValues=new ContentValues();
        contentValues.put("foodname",food.getFoodname()==null?"空":food.getFoodname());
        contentValues.put("foodprice",food.getFoodprice().toString()==null?"1":food.getFoodprice().toString());
        contentValues.put("count",food.getCount().toString()==null?"1":food.getCount().toString());
        sqLiteDatabase.insert(TB_FOOD,null,contentValues);
    }

    @Override
    public void deleteFood(String [] _id) {
        sqLiteDatabase.delete(TB_FOOD,"_id=?",_id);
    }

    @Override
    public void updateFood(Food food) {
        ContentValues contentValues=new ContentValues();

        //通过反射排查null
        Field[] fields=Food.class.getDeclaredFields();
        for(Field field:fields){
            String key=field.getName();
            field.setAccessible(true);
            if(!key.equals("_id")) {
                try {
                    Object value=field.get(food);
                    if(value!=null){
                        contentValues.put(key,String.valueOf( value));
                    }
                }catch (Exception e){}
            }
        }
        System.out.println(contentValues);
        sqLiteDatabase.update(TB_FOOD,contentValues,"_id=?",new String[]{String.valueOf(food.get_id())});
    }

    @Override
    public List<Food> queryFoodByFood(Food food) {
        List<Food> foods=new ArrayList<>();
        Cursor cursor;

        if(food==null){
            cursor= sqLiteDatabase.query(TB_FOOD,null,null,null,null,null,null);
            while (cursor.moveToNext()){
                Food f=new Food();
                f.set_id(cursor.getInt(cursor.getColumnIndex("_id")));
                f.setCount(cursor.getInt(cursor.getColumnIndex("count")));
                f.setFoodname(cursor.getString(cursor.getColumnIndex("foodname")));
                f.setFoodprice(cursor.getDouble(cursor.getColumnIndex("foodprice")));
                foods.add(f);
            }

            return foods;
        }

        StringBuilder selection=new StringBuilder();
        ArrayList<String> args=new ArrayList<>();

        //通过反射排查null
        Field [] fields=Food.class.getDeclaredFields();
        int i=0;
        for(Field field:fields){
            String key=field.getName();
            field.setAccessible(true);
            try {
                Object value=field.get(food);
                if(value!=null){
                    //构造条件
                    if(i==0){
                        selection.append(key +" like ?");
                    }else{
                        selection.append("and "+key +" like ?");
                    }
                    //获得参数
                    args.add((String)value);
                    i++;
                }
            }catch (Exception e){
                Log.d("dao",e.getMessage());
            }
        }

        String selectionArgs[]=args.toArray(new String[args.size()]);

        if(selection.toString().isEmpty()){
            cursor= sqLiteDatabase.query(TB_FOOD,null,null,null,null,null,null);
        }else{
            cursor=sqLiteDatabase.query(TB_FOOD,null,selection.toString(),selectionArgs,null,null,null);
        }



        while (cursor.moveToNext()){
            Food f=new Food();
            f.set_id(cursor.getInt(cursor.getColumnIndex("_id")));
            f.setCount(cursor.getInt(cursor.getColumnIndex("count")));
            f.setFoodname(cursor.getString(cursor.getColumnIndex("foodname")));
            f.setFoodprice(cursor.getDouble(cursor.getColumnIndex("foodprice")));
            foods.add(f);
        }

        return foods;
    }
}
